package com.jinmdz.fmis.dao.dao.provider;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Date;


/**
 * 守灵Provider
 *
 * @author LiCongLu
 * @date 2020-02-16 16:10
 */
public class WakeMournProvider {

    /**
     * 按照业务编码查询礼厅信息
     *
     * @param id 主键
     * @return
     * @author LiCongLu
     * @date 2020-02-20 14:59
     */
    public String getWakeMournViewById(@Param("id") Integer id) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" mourn_ceremony.id,business_code,mourn_ceremony.hall_type_code,hall_id ");
                builder.append(" ,appointment_in_time,appointment_out_time,reality_in_time,reality_out_time ");
                builder.append(" ,process_state_code,rule_number,rule_unit,rule_code,rule_remark,mourn_ceremony.remark ");
                builder.append(" ,mourn_ceremony.business_type,random_code,charge_id,mourn_ceremony.version ");
                builder.append(" ,mourn_ceremony_hall.hall_code,mourn_ceremony_hall.hall_name ");
                SELECT(builder.toString());
                FROM(" mourn_ceremony ");
                LEFT_OUTER_JOIN(" mourn_ceremony_hall on mourn_ceremony_hall.id = mourn_ceremony.hall_id  ");
                WHERE(" mourn_ceremony.deleted = 0 ");
                WHERE(" mourn_ceremony.id = #{id} ");
                ORDER_BY(" mourn_ceremony.id ");
            }
        }.toString();
    }

    /**
     * 按照业务编码查询礼厅信息
     *
     * @param businessCode 业务编码
     * @return
     * @author LiCongLu
     * @date 2020-02-16 16:09
     */
    public String listWakeMournViewByBusinessCode(@Param("businessCode") String businessCode) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" mourn_ceremony.id,business_code,mourn_ceremony.hall_type_code,hall_id ");
                builder.append(" ,appointment_in_time,appointment_out_time,reality_in_time,reality_out_time ");
                builder.append(" ,process_state_code,rule_number,rule_unit,rule_code,rule_remark,mourn_ceremony.remark ");
                builder.append(" ,mourn_ceremony.business_type,random_code,charge_id,mourn_ceremony.version ");
                builder.append(" ,mourn_ceremony_hall.hall_code,mourn_ceremony_hall.hall_name ");
                SELECT(builder.toString());
                FROM(" mourn_ceremony ");
                LEFT_OUTER_JOIN(" mourn_ceremony_hall on mourn_ceremony_hall.id = mourn_ceremony.hall_id  ");
                WHERE(" mourn_ceremony.deleted = 0 ");
                WHERE(" mourn_ceremony.business_code = #{businessCode} ");
                ORDER_BY(" mourn_ceremony.id ");
            }
        }.toString();
    }


    /**
     * 通过预约时间等条件获取占用礼厅业务主键
     *
     * @param hallId             设备主键
     * @param businessId         原告别守灵信息主键
     * @param outState           出厅状态
     * @param inState            入厅状态
     * @param appointmentInTime  新预约入厅时间
     * @param appointmentOutTime 新预约出厅时间
     * @return
     * @author LiCongLu
     * @date 2020-02-26 17:28
     */
    public String getBusinessIdForHallId(@Param("hallId") Integer hallId, @Param("businessId") Integer businessId, @Param("outState") Integer outState, @Param("inState") Integer inState
            , @Param("appointmentInTime") Date appointmentInTime, @Param("appointmentOutTime") Date appointmentOutTime) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" id ");
                SELECT(builder.toString());
                FROM(" mourn_ceremony ");
                WHERE(" deleted = 0  ");
                WHERE(" hall_id = #{hallId} and id != IFNULL(#{businessId},0) ");
                WHERE(" process_state_code != #{outState} and appointment_in_time is not null ");

                // 判断条件
                StringBuilder whereExt = new StringBuilder();

                // 原预约出厅时间为空
                whereExt.append(" appointment_out_time is null ");

                // 原流程状态值
                whereExt.append(" or process_state_code = #{inState} ");

                // 原预约入厅时间小于新入厅时间，原预约出厅时间大于新入厅时间
                if (appointmentInTime != null) {
                    whereExt.append(" or ( appointment_in_time <= #{appointmentInTime,jdbcType=TIMESTAMP} and appointment_out_time >= #{appointmentInTime,jdbcType=TIMESTAMP} )");
                }

                // 原预约入厅时间小于新出厅时间，原预约出厅时间大于新出厅时间
                if (appointmentOutTime != null) {
                    whereExt.append(" or ( appointment_in_time <= #{appointmentOutTime,jdbcType=TIMESTAMP} and appointment_out_time >= #{appointmentOutTime,jdbcType=TIMESTAMP} )");
                }

                // 追加判断条件
                WHERE(" ( " + whereExt.toString() + " ) ");

                // 取预约入厅时间排序第一个业务信息
                ORDER_BY(" mourn_ceremony.appointment_in_time limit 1 ");
            }
        }.toString();
    }
}
